package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.db.RecommendUser;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TodayBestService {
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private RecommendUserApi recommendUserApi;
    @DubboReference
    private UserLikeApi userLikeApi;
    @Autowired
    private MessagesService messagesService;


    public List<TodayBest> queryCardsList() {
        //获取用户数据
        long userId1 = UserHolder.getUserId();
        List<RecommendUser> records = recommendUserApi.findAll( UserHolder.getUserId(), 0, 10 );
        List<RecommendUser> newRecommendUserList = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
             createRecommendUser(newRecommendUserList, records,i);//递归排除重复数
        }

        List<Long> userIds = CollUtil.getFieldValues(newRecommendUserList,"userId",Long.class);
        System.out.println(userIds);
        Map<Long, UserInfo> map = userInfoApi.findAll( userIds );
        List<TodayBest> result = new ArrayList<>();
        for (Long userId : userIds) {
            UserInfo userInfo = map.get( userId );
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getId());
            todayBest.setAge(userInfo.getAge());
            todayBest.setAvatar(userInfo.getAvatar());
            todayBest.setGender(userInfo.getGender());
            todayBest.setNickname(userInfo.getNickname());
            todayBest.setTags( userInfo.getTags().split( "," ));
            result.add(todayBest);
        }
        return result;
    }

    private List createRecommendUser(List<RecommendUser> newRecommendUserList, List<RecommendUser> records,
                                       int a ) {
        RecommendUser remove = records.remove(9-a);
            if ( remove.getUserId()!=UserHolder.getUserId()){
                newRecommendUserList.add(remove);
            }
        return newRecommendUserList;
    }


    //右滑喜欢
    public Boolean likeUser(Long likeUserId) {
        long userId1 = UserHolder.getUserId();
        String id =userLikeApi.saveUserLike(userId1, likeUserId);
        if (StringUtils.isEmpty( id )) {
            return false;
        }
        if (this.userLikeApi.isMutualLike(userId1, likeUserId)) {
            //相互喜欢成为好友,调用环信添加好友方法
            messagesService.contacts( likeUserId );
        }
        return true;
    }
    //删除喜欢
    public Boolean disLikeUser(Long likeUserId) {
        return userLikeApi.deleteUserLike( UserHolder.getUserId(),likeUserId );
    }
}
